package com.r2coding.article.api;

import com.r2coding.article.entity.CommentEntity;
import com.r2coding.article.service.CommentService;
import com.r2coding.article.vo.resp.CommentByArticleIdRespVo;
import com.r2coding.article.vo.resp.CommentRespVo;
import com.r2coding.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author:98k灬
 * @since:2022/05/03 18:13
 **/
@RestController
@RequestMapping("/api/comment")
public class ApiCommentController {
    @Autowired
    CommentService commentService;

    /**
     * 根据文章id查询评论信息
     *
     * @param articleId 文章id
     * @return 评论信息
     */
    @GetMapping("/getByArticleId/{articleId}")
    public Result getByArticleId(@PathVariable("articleId") Integer articleId) {
        //List<CommentEntity> list = commentService.getByArticleId(articleId);
        CommentRespVo resp = commentService.commentInfo(articleId);
        return Result.ok(resp);
    }

    /**
     * 发表评论
     *
     * @param commentEntity 评论信息
     * @return 统一响应体
     */
    @PostMapping("/addComment")
    public Result addComment(@RequestBody CommentEntity commentEntity) {
        /*commentEntity.setCreateTime(new Date());
        commentService.save(commentEntity);*/
        commentService.saveComment(commentEntity);
        return Result.ok();
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return 统一响应体
     */
    @DeleteMapping("/deleteComment/{id}")
    public Result deleteComment(@PathVariable("id") Integer id) {
        commentService.removeById(id);
        return Result.ok();
    }
}
